/*
* Valentine Gladstein
* SOLAR ID 108731583
* Homework #1
* CSE214
* Recitation 03-Phil Ammirato 
* Grading TA:Phil Ammirato 
*/

import java.io.*;
import java.util.*;

public class UserManagerDriver {

    //The USerManagerDriver class provides a console-driven menu system for manipulating a USerMAnager object

    private UserManager manager;    //Stores the UserManager object associated with this UserManagerDriver
    private UserManager backup;     //Stores a back-up copy of the USerManager object 



/**
 * Displays the main menu in the standard output and gets the user's input selection.
 * @return The user's entry, converted to lowercase.
 */
    public String displayMenu(){

	System.out.println("\n\n---------------------------");
	System.out.println("Main Menu");
	System.out.println("---------------------------");
	System.out.println("A) Add user");
	System.out.println("B) Backup (Save)");
	System.out.println("G) Get user");
	System.out.println("L) Login");
	System.out.println("P) Print all users");
	System.out.println("R) Remove user");
	System.out.println("S) Size");
	System.out.println("V) Validate");
	System.out.println("Q) Quit");
	System.out.println("---------------------------\n");
	System.out.println("");
	System.out.print("Please make a selection: ");

	String str = getInput();

	return str.toLowerCase();

    }



/**
 * A helper method used internally to read user input from the standard input
 * @return a string containing the user's input
 */
    public String getInput() {
	String s = "";
	InputStreamReader isr = new InputStreamReader(System.in);
	BufferedReader br = new BufferedReader(isr);
	try {
	    s = br.readLine();

	} catch (IOException e) {}
	return s;
    }
   


/**
 * Provides a menu-driven system for adding a USer object to the UserManager. After prompting the user to enter parameters, this method calls the USerManager's addUSer() method.
 *
 */ 
    public void addUser() {

	String username;
	String password;
	String name;
	String email;

	System.out.println("\n");
	System.out.print("Enter Username: ");
	username = getInput();
	System.out.print("Enter password: ");
	password = getInput();
	System.out.print("Ente name: ");
	name = getInput();
	System.out.print("Enter email: ");
	email = getInput();

	User usr = new User(username, password, name, email);
	
	try {
	    manager.addUser(usr, manager.size());
	}
	catch (Exception e) {
	    System.out.println("An error occured while adding the user. This may be because this UseManage is full. User " + username + " has not been added.");
	    return;
	}
	System.out.println("\nUser added: " + username) ;
    }



/**
 * Calls the USerManager's clone() method to store a copy of the UserManager in the backup aprameter.
 */ 
    public void backup() {
	backup = (UserManager)(manager.clone());
	System.out.println("\nBackup saved successfully!");
    }


/**
 * Provides a menu-driven system for retrieving a User object from the UserManager. After 
 * prompting the user to enter parameters, this method calls the UserManager's getUser() method.
 *
 */ 
    public void getUser() {
	System.out.println("\nEnter the position: ");
	int in;
	try {
	    in = Integer.parseInt(getInput());
	}
	catch(Exception e){
	    System.out.println("Please enter an integer.");
	    return;
	}
	try {
	    String s = manager.getUser(in).toString();
	    System.out.println("\nUser data for user at position " + in + ":");
	    System.out.println(s);
	}    
	catch (Exception e) {
	    System.out.println("The number entered is beyond this UserManager's bounds. There are currently " + manager.size() + " users in this manager. Please try again.");
	    return;
	}
    }

/**
 * Provides a menu-driven system for checking login credentials for the USerManager. 
 * After prompting the user to enter parameters, this method calls the UserManager's login() method.
 *
 */ 
    public void login() {
	String username;
	String password;

	System.out.print("\nEnter username: ");	
	username = getInput();
	System.out.print("Enter password: ");
	password = getInput();

	manager.login(username, password);
    }


/**
 * Provides a menu-driven system for printing a formatted representation of the UserManager 
 * object to the standard output.This method calls the UserManager's getUser() method.
 *
 */ 
    public void printUsers() {
	manager.printAllUsers();
    }

/**
 * Provides a menu-driven system for removing a User object from the UserManager. 
 * After prompting the user to enter parameters, this method calls the UserManager's 
 * removeUser() method.
 *
 */ 
    public void removeUser() {
	System.out.print("\nRemove user at which position? ");
	int in;
	try {
	    in = Integer.parseInt(getInput());
	}
	catch(Exception e){
	    System.out.println("\nPlease enter an integer.");
	    return;
	}
	try {
	    manager.removeUser(in);
	    System.out.println("\nUser at position " + in + " has been removed.");
	}    
	catch (Exception e) {
	    System.out.println("\nThe number entered is beyond this UserManager's bounds. There are currently " + manager.size() + " users in this manager. Please try again.");
	    return;
	}
    }


/**
 * Provides a menu-driven system for getting the number of User objects in the current UserManager. 
 * This method calls the UserManager's size() method.
 *
 */ 
    public void size() {
	System.out.println("\nThere are currently " + manager.size() + " users in this manager.");
    }
    
/**
 * Provides a menu-driven system for comparing the current UserManager object to the backup 
 * copy of the UserManager. This method calls the UserManager's equals() method to detemine equality.
 *
 */ 
    public void validate() {
	boolean result = manager.equals(backup);
        String output = ("Backup and user manager are ");
	if (!result) 
	    output += "not ";
	output += "equal.";
	System.out.println("\n" + output);
    }

/**
 * Provides a way for the user to quit the program gracefully
 *
 */ 
    public void quit() {
	System.out.println("\nTerminating program normally....\n\n");
	System.exit(0);
    }

    public UserManagerDriver() {
	manager = new UserManager();
    }

/**
 * Provides a menu-driven system for manipulating the UserManager. This method loops back 
 * to displaying the method after every action the user takes.
 *
 */ 
    public static void main(String[] args) {
	UserManagerDriver driver = new UserManagerDriver();	
	while (true){
	    String str = driver.displayMenu();
	    if (str.equals("a"))
		driver.addUser();
	    if (str.equals("b"))
		driver.backup();
	    if (str.equals("g"))
		driver.getUser();
	    if (str.equals("l"))
		driver.login();
	    if (str.equals("p"))
		driver.printUsers();
	    if (str.equals("r"))
		driver.removeUser();
	    if (str.equals("s"))
		driver.size();
	    if (str.equals("v"))
		driver.validate();
	    if (str.equals("q"))
		driver.quit();
	}
	
    }
}
